//using System;
//using System.Collections.Generic;
//using FireStarterModeller.BO;
//using Habanero.Base;
//using Habanero.BO;
//using Habanero.BO.ClassDefinition;
//using Habanero.UI.Base;

//namespace FireStarterModeller.UI
//{
//    public class ExtendedComboBoxMapper : IControlMapper
//    {
//        private LookupComboBoxMapper _lookupComboBoxMapper;
//        private ExtendedComboBox _extendedComboBox;
//        private IFormHabanero _popupForm;

//        public ExtendedComboBoxMapper(IControlHabanero ctl, string propName, bool isReadOnly, IControlFactory controlFactory)
//        {
//            _extendedComboBox = (ExtendedComboBox)ctl;
//            _lookupComboBoxMapper = new LookupComboBoxMapper(_extendedComboBox.ComboBox, propName, isReadOnly, controlFactory);
//            _extendedComboBox.Button.Click += delegate
//            {
//                ShowPopupForm();
//                _popupForm.Closed += delegate
//                                         {
//                            IGridAndBOEditorControl gridAndBOEditorControl = (IGridAndBOEditorControl)_popupForm.Controls[0];
//                            IBusinessObject currentBusinessObject = gridAndBOEditorControl.CurrentBusinessObject;
//                            if ((currentBusinessObject != null) && currentBusinessObject.IsValid())
//                            {
//                                currentBusinessObject.Save();
//                            }
//                            ReloadLookupValues();
//                            if (currentBusinessObject != null)
//                            {
//                                _extendedComboBox.ComboBox.SelectedValue = currentBusinessObject;
//                            }
//                        };
//                _popupForm.ShowDialog();

//            };

//        }

//        private void ReloadLookupValues()
//        {
//            Type classType;
//            GetLookupTypeClassDef(out classType);
//            IBusinessObjectCollection collection = GetCollection(classType);
//            _lookupComboBoxMapper.LookupList.Clear();
//            Dictionary<string, string> lookupValues = new Dictionary<string, string>();
//            foreach (IBusinessObject businessObject in collection)
//            {
//                lookupValues.Add(businessObject.ToString(), businessObject.ID.ToString());
//            }
//            _lookupComboBoxMapper.LookupList = lookupValues;
//        }

//        public void ShowPopupForm()
//        {
//            //IDefaultBOEditorForm defaultBOEditorForm = new DefaultBOEditorStub((BusinessObject) this.BusinessObject,"default",controlFactory);
//            Type classType;
//            ClassDef lookupTypeClassDef = GetLookupTypeClassDef(out classType);
//            _popupForm = ControlFactory.CreateForm();
//            _popupForm.Height = 600;
//            _popupForm.Width = 800;
//            Type classSpecificControlType = typeof(GridAndBOEditorControl<>).MakeGenericType(classType);
//            IBusinessObjectControlWithErrorDisplay boEditorPanel = new BOEditorPanel(lookupTypeClassDef, "default", ControlFactory);
//            IGridAndBOEditorControl gridAndBoEditorControl = (IGridAndBOEditorControl)Activator.CreateInstance(classSpecificControlType, ControlFactory, boEditorPanel);
//            gridAndBoEditorControl.Dock = DockStyle.Fill;
//            _popupForm.Controls.Add(gridAndBoEditorControl);
//            IBusinessObjectCollection col = GetCollection(classType);
//            gridAndBoEditorControl.SetBusinessObjectCollection(col);
//        }

//        private ClassDef GetLookupTypeClassDef(out Type classType)
//        {
//            BOMapper mapper = new BOMapper(BusinessObject);
//            ClassDef lookupTypeClassDef = mapper.GetLookupListClassDef(_lookupComboBoxMapper.PropertyName);
//            classType = lookupTypeClassDef.ClassType;
//            return lookupTypeClassDef;
//        }

//        private IBusinessObjectCollection GetCollection(Type classType)
//        {
//            Type collectionType = typeof(BusinessObjectCollection<>).MakeGenericType(classType);
//            IBusinessObjectCollection col = (IBusinessObjectCollection)Activator.CreateInstance(collectionType);
//            col.LoadAll();
//            return col;
//        }

//        public LookupComboBoxMapper LookupComboBoxMapper
//        {
//            get { return _lookupComboBoxMapper; }
//            //internal set { _lookupComboBoxMapper = value; }
//            set { _lookupComboBoxMapper = value; }
//        }

//        public IControlHabanero Control
//        {
//            get { return _extendedComboBox; }
//        }

//        public string PropertyName
//        {
//            get { return _lookupComboBoxMapper.PropertyName; }
//        }

//        public IBusinessObject BusinessObject
//        {
//            get { return _lookupComboBoxMapper.BusinessObject; }
//            set { _lookupComboBoxMapper.BusinessObject = value; }
//        }

//        public IErrorProvider ErrorProvider
//        {
//            get { return _lookupComboBoxMapper.ErrorProvider; }
//        }

//        public bool IsReadOnly
//        {
//            get { return _lookupComboBoxMapper.IsReadOnly; }
//        }

//        public IControlFactory ControlFactory
//        {
//            get { return _lookupComboBoxMapper.ControlFactory; }
//        }

//        public IFormHabanero PopupForm
//        {
//            get { return _popupForm; }
//        }

//        public void ApplyChangesToBusinessObject()
//        {
//            _lookupComboBoxMapper.ApplyChangesToBusinessObject();
//        }

//        public void UpdateControlValueFromBusinessObject()
//        {
//            _lookupComboBoxMapper.UpdateControlValueFromBusinessObject();
//        }
//    }
//}